package org.weasel.controllers;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.weasel.business.Developer;
import org.weasel.business.Project;
import org.weasel.business.UserStory;
import org.weasel.tree.TreeObjectUserStory;
import org.weasel.views.DeveloperComposite;
import org.weasel.views.TaskListComposite;
import org.weasel.views.UserStoryDetail;

public class UserStoryViewController {
	private static UserStoryViewController instance = null;
	private ApplicationController appController;
	private TaskListController taskController;
	private DeveloperCompositeController developerCompositeController;
	private TreeObjectUserStory to_userStory;
	private UserStoryDetail userStoryView;
	private Project projectOfUserStory;
	
	private UserStoryViewController(){
		
	}
	
	public static UserStoryViewController instance(){
		if(instance == null){
			instance = new UserStoryViewController();
		}
		return instance;
	}
	
	public void setApplicationController(ApplicationController appController){
		this.appController = appController;
	}
	
	/**
	 * This is needed for retrieving the project developers later
	 * @param project
	 */
	public void setProjectOfUserStory(Project project) {
		this.projectOfUserStory = project;
	}
	
	public void setUserStory(TreeObjectUserStory story){
		this.to_userStory = story;
	}
	
	public UserStory getUserStory(){
		return to_userStory.getUserStory();
	}
	
	public Project getProject(){
		return projectOfUserStory;
	}
	
	public TaskListComposite createTaskListComposite(Composite parent, int style){
		taskController = new TaskListController(this);
		return taskController.createTaskListComposite(parent, style);
	}
	
	public void showUserStoryView(){
//		attach the appropriate DeveloperComposite controller
		if(developerCompositeController==null){
			developerCompositeController = new DeveloperCompositeController();
			developerCompositeController.setUserStoryViewController(this);
			developerCompositeController.initializeLists(projectOfUserStory.getDevelopers(), to_userStory.getUserStory().getDevelopers());
		}else{
			developerCompositeController.initializeLists(projectOfUserStory.getDevelopers(), to_userStory.getUserStory().getDevelopers());
			developerCompositeController.refreshComposite();
			taskController.refreshComposite();
		}
		try {
//			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(UserStoryDetail.ID, to_userStory.getUserStory().getId(),IWorkbenchPage.VIEW_ACTIVATE);
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(UserStoryDetail.ID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
//		IViewReference ref =  page.findViewReference(UserStoryDetail.ID,to_userStory.getUserStory().getId());
//		IViewPart viewPart = ref.getView(false);
//		userStoryView = (UserStoryDetail) viewPart;
		userStoryView = (UserStoryDetail) page.findView(UserStoryDetail.ID);
		userStoryView.showUserStoryInformation(to_userStory.getUserStory());
	}
	
	/**
	 * Needed to update the view if something changes in the model,
	 * i.e. developers are deleted
	 *
	 */
	public void updateStoryViews(){
		//handle multiple views here! Traverse a collection where views are kept and
		//call the method below
		if(userStoryView!=null){
			if(developerCompositeController!=null) developerCompositeController.refreshComposite();
			userStoryView.showUserStoryInformation(to_userStory.getUserStory());
		}
	}

	public void updateStoryName(String name) {
		to_userStory.getUserStory().setName(name);
		to_userStory.setName(name);
		appController.refreshTreeViewer();
		appController.changesOccured();
	}

	public void updateStoryDescription(String description) {
		to_userStory.getUserStory().setDescription(description);
		appController.changesOccured();
	}

	public void updateStoryBusinessValue(String businessValue) {
		to_userStory.getUserStory().setBusinessValue(Integer.parseInt(businessValue));
		appController.changesOccured();
	}

	public void updateStoryRisk(String risk) {
		to_userStory.getUserStory().setRisk(Integer.parseInt(risk));
		appController.changesOccured();
	}

	public void updateStoryEstimatedTime(String estimatedTime) {
		//check if it is a valid time format
//		try {
			Time t = Time.valueOf(estimatedTime);
			to_userStory.getUserStory().setEstimatedTime(t);
//			userStoryView.getLblEstimatedTimeInfo().setVisible(false);
//			userStoryView.changeEstimatedTimeVisibility(false);
//		} catch (Exception e) {
////			userStoryView.getLblEstimatedTimeInfo().setVisible(true);
//			userStoryView.changeEstimatedTimeVisibility(true);
//		}
		appController.changesOccured();
	}

	public void updateStoryUsedTime(String usedTime) {
		//TODO check here if entered time is in a valid format
		to_userStory.getUserStory().setUsedTime(Time.valueOf(usedTime));
		if(!usedTime.equals("00:00:00")){
			to_userStory.getUserStory().setFinishedDate(new SimpleDateFormat("dd.MM.yyyy").format(new Date()));
		}else{
			to_userStory.getUserStory().setFinishedDate(null);
		}
		//this line is needed so that the correct images are shown in the tree
		appController.refreshTreeViewer();
		appController.changesOccured();
	}

	public DeveloperComposite createDeveloperComposite(Composite parent, int style) {
		return developerCompositeController.loadDeveloperComposite(parent, style);
	}

	public void removeDeveloper(Developer developer) {
		to_userStory.getUserStory().removeDeveloper(developer.getId());
		appController.changesOccured();
	}

	public void addDeveloper(Developer developer) {
		to_userStory.getUserStory().addDeveloper(developer);
		appController.changesOccured();		
	}
	
	
	/**
	 * This is called, when the Composite of the View
	 * is disposed...we need to do some stuff to be
	 * able to startup the view the next time successfully
	 *
	 */
	public void doViewClosedActions() {
		this.developerCompositeController = null;
	}
}
